﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HelloMVC.Model;
using HelloMVC.Service;
using HelloMVC.Interface.Repository;

namespace HelloMVC.Areas.DashBoard.Controllers
{
    public class IssueController : Controller
    {

        #region Private Var
        private readonly IIssueRepository _issueRepository;
        #endregion


        public IssueController(IIssueRepository issueRepository)
        {
            _issueRepository = issueRepository;
        }

        //
        // GET: /DashBoard/Issue/
        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /DashBoard/Issue/Create
        public ActionResult Create()
        {
            var issue = new Issue { Priority = "minor" };
            return View(issue);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(Issue issue)
        {
            if (!ModelState.IsValid)
            {
                return View(issue);
            }

            return RedirectToAction("Index");
        }

        /// <summary>
        /// Autocomplete service action
        /// </summary>
        /// <param name="name">Service name</param>
        /// <param name="q">typed value</param>
        /// <returns></returns>
        public String AutocompleteService(String name, String q, String rel)
        {
            if (name.Equals("component"))
                return String.Format("{0}-UI\n{0}-DB\n{0}-BugFix", rel);
            else
                return String.Format("{0}-V1.1\n{0}-V1.2\n{0}-V1.3", rel);
        }

        [JsonFilter(Param = "request", JsonDataType = typeof(DatatableRequestModel))]
        public JsonResult QueryIssues(DatatableRequestModel request)
        {
            var issues = _issueRepository.QueryIssues(new IssueQuery { Start = request.iDisplayStart, Length = request.iDisplayLength });
            var count = _issueRepository.QueryIssuesCount(null);
            var response = new DatatableResponseModel
            {
                sEcho = request.sEcho,
                iTotalDisplayRecords = count,
                iTotalRecords = count,
                aaData = new List<IList<String>>()
            };

            foreach (Issue issue in issues)
            {
                response.aaData.Add(new[] { 
                    issue.Id.ToString(),        // Issue Id
                    issue.Type,                 // Issue Type
                    issue.Project + "-" + issue.Id,   // Issue Key
                    issue.Summary,              // Issue Summary        
                    issue.Assignee,             // Issue Assignee 
                    "issue.Reporter",           // Issue Reporter
                    issue.Priority,             // Issue Priority 
                    issue.Status,               // Issue Status 
                    "issue.Resolution",         // Issue Resolution 
                    "2011-2-28",                // Issue Created
                    "2011-2-8"                  // Issue Updated
                });
            }
                
            return Json(response);
        }


        #region Dropdown items
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public PartialViewResult ViewOptions()
        {
            return PartialView("ViewOptionsDrop");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public PartialViewResult ToolOptions()
        {
            return PartialView("ToolOptionsDrop");
        }
        #endregion
    }
}
